﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Events;
using MVVMDialogs;
using Hockey.Shared;
using Hockey.UILayer.Forms;
using System.Windows.Controls;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Unity;

namespace Hockey.UILayer.ViewModels
{
    /// <summary>
    /// Player Label ViewModel
    /// </summary>
    public class PlayerLabelVM: NotificationObject
    {
        private IDialogService _dlgService;
        private IEventAggregator _events;
        private IUnityContainer _unity;

        /// <summary>
        /// Constructor
        /// </summary>
        public PlayerLabelVM(IDialogService dlgService , IEventAggregator events, IUnityContainer unity)            
        {
            if (dlgService == null)
                throw new ArgumentNullException("Dialog Service");
            _dlgService = dlgService;

            if (events == null)
                throw new ArgumentNullException("Events");
            _events = events;

            if (unity == null)
                throw new ArgumentNullException("Unity");
            _unity = unity;

             // register events
            _events.GetEvent<EnterPlayerName>().Subscribe((obj) =>
                {
                    // check whether it is our view
                    if ((obj as UserControl).DataContext == this)
                    {
                        EnterNewName();
                    }

                }, true);

            // event for setting the player name
            _events.GetEvent<SetPlayerName>().Subscribe((rec) =>
                {
                    if (rec.PuckDirection == PuckDirection)
                    {
                        PlayerName = rec.PlayerName;
                        RaisePropertyChanged("PlayerName");
                    }
                }, true);

            // event for returning the player name
            _events.GetEvent<GetPlayerName>().Subscribe((direction) =>
                {
                    if (direction == PuckDirection)
                    {
                        _events.GetEvent<ReturningPlayerName>().Publish(PlayerName);
                    }
                }, true);
        }

        /// <summary>
        /// Binding Property for player name
        /// </summary>
        public string PlayerName
        { get; set; }

        /// <summary>
        /// What puck direction do we have?
        /// </summary>
        public ePuckDir PuckDirection
        { get; set; }


        /// <summary>
        /// Handler responsible for running the form to enter the new name
        /// </summary>
        private void EnterNewName()
        {
            PlayerNameFormVM vm = _unity.Resolve<PlayerNameFormVM>();
            vm.PlayerName = PlayerName;
            _dlgService.ShowDialog<EnterPlayerNameForm>(this, vm);
            if (vm.Success)
            {
                PlayerName = vm.PlayerName;
                RaisePropertyChanged("PlayerName");
            }
        }


    }
}
